package  
{
	import net.flashpunk.Entity;
	import net.flashpunk.graphics.Spritemap;
	import net.flashpunk.FP;
	import net.flashpunk.tweens.misc.VarTween;
	import net.flashpunk.utils.Input;
	import net.flashpunk.utils.Key;
	import flash.external.ExternalInterface;
	/**
	 * © 2012 TheJavaArcade.com
	 */
	public class Kong extends Entity {
		private var kongSprite:Spritemap = new Spritemap(Assets.KONG_SPRITE, 48, 31);
		public var level:int;
		private var theWorld:GameEngine;
		
		public function Kong(theLevel:int, world:GameEngine) {
			level = theLevel;
			kongSprite.add("kong", [0, 0, 0, 1, 2, 1, 2, 1, 2, 0, 0, 1, 2, 1, 2, 1, 0], 5, true);
			kongSprite.add("free", [3, 4, 5, 6], 5, true);
			graphic = kongSprite;
			kongSprite.play("kong");
			type = "kong";
			setHitbox(80, 31, 16, 0);
			x = 840;
			y = 489;
			layer = 1;
			theWorld = world;
		}
		override public function update():void {
			if (GameEngine.level == level && collide("fracas", x , y) && GameEngine.cagekey.state != 2) {
				GameEngine.kongDialogue.visible = true;
				GameEngine.kongDialogue.x = (x +24) - (GameEngine.kongDialogue.theText.width / 2);
				GameEngine.kongDialogue.y = y - 25;
			}else if(GameEngine.fracas.gameOver == false){
				GameEngine.kongDialogue.visible = false;
			}
			
			if (GameEngine.level == level && collide("fracas", x , y) && GameEngine.fracas.gameOver == false && GameEngine.cagekey.state == 2) {
				//what happens when you win the game
				GameEngine.fracas.gameOver = true;
				GameEngine.score = GameEngine.score + GameEngine.timer;
				fadeOut();
			}
			if (Input.pressed(Key.ENTER) && GameEngine.fracas.gameOver == true) {
				try {
					ExternalInterface.call("insertScore","mfa",GameEngine.score.toString(),null, NameText.nameStr.substr(0, NameText.letterCount));
				}catch (e:Error) { }
				Assets.userNameStr = NameText.nameStr.substr(0, NameText.letterCount);
				FP.world = new TitleWorld;
			}
		}
		public function fadeOut():void {
			var vineTween:VarTween = new VarTween;
			vineTween.tween(GameEngine.vine[0].vines, "alpha", 0, 1.5);
			addTween(vineTween, true);
			
			var areaTween:VarTween = new VarTween;
			areaTween.tween(GameEngine.area[0].tiles, "alpha", 0, 1.5);
			addTween(areaTween, true);
			
			var backgroundTween:VarTween = new VarTween;
			backgroundTween.tween(GameEngine.background.bg_1, "alpha", 0, 1.5);
			addTween(backgroundTween, true);
			
			var barTween:VarTween = new VarTween(setMonkeys);
			barTween.tween(GameEngine.strengthBar.bar, "alpha", 0, 1.5);
			addTween(barTween, true);
		}		
		public function setMonkeys():void {
			GameEngine.fracas.fracasSprite.play("stand");
			kongSprite.play("free");
			GameEngine.kongDialogue.theText.text = "Thank You!";
			GameEngine.kongDialogue.visible = true;
			GameEngine.kongDialogue.x = (x +24) - (GameEngine.kongDialogue.theText.width / 2);
			GameEngine.kongDialogue.y = y - 25;
			
			//set the score & text
			GameEngine.gameoverDialogue.theText.text = "final score";
			GameEngine.gameoverDialogue.theText.size = 16;
			GameEngine.gameoverDialogue.x = FP.camera.x + (FP.halfWidth / 2) - (GameEngine.gameoverDialogue.theText.width / 2);
			GameEngine.gameoverDialogue.y = FP.camera.y + 10;
			
			GameEngine.scoreDialogue.theText.text = GameEngine.score.toString();
			GameEngine.scoreDialogue.theText.size = 16;
			GameEngine.scoreDialogue.x = FP.camera.x + (FP.halfWidth/2) - (GameEngine.scoreDialogue.theText.width / 2);
			GameEngine.scoreDialogue.y = FP.camera.y + 30;
			
			//enter name stuff
			GameEngine.nameDialogue.theText.text = "enter your name";
			GameEngine.nameDialogue.x = FP.camera.x + (FP.halfWidth / 2) - (GameEngine.nameDialogue.theText.width / 2);
			GameEngine.nameDialogue.y = FP.camera.y + 68;
			
			GameEngine.enterDialogue.theText.text = "press ENTER when complete";
			GameEngine.enterDialogue.x = FP.camera.x + (FP.halfWidth / 2) - (GameEngine.enterDialogue.theText.width / 2);
			GameEngine.enterDialogue.y = FP.camera.y + 100;
			
			theWorld.add(new NameText);
		}
		
	}

}